<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Validate;

class Dlist extends Controller
{
    public function Login()
    {
        $data = request()->post();
        $rule = [
                'user_name'  => 'require|/^[a-z0-9_-]{5,10}$/',
                'user_pwd'   => 'require|/^[\S]{6,12}$/',
                
            ];
            
            $msg = [
                'user_name.require' => '名称必填',
                'name./^[a-z0-9_-]{5,10}$/'     => '用户名长度为5-10位字符',
                'user_pwd.require' => '密码必填',
                'user_pwd./^[\S]{6,12}$/'     => '密码必须6到12位，且不能出现空格',  
            ];
            
        
            
            $validate = new Validate($rule, $msg);
            $result   = $validate->check($data);
            if( !$result )
            {
                return json(array('code' => 1, 'msg' => $validate->getError(), 'data' => ''));
            }

            $where[ 'user_name' ] = $data[ 'user_name' ];
            $res = model( 'User' ) -> Login( $where );
            if( !$res )
            {
                return json(array('code' => 1, 'msg' => "登录失败!!!!", 'data' => ''));
            }
            if( $res[ 'user_pwd' ] != $data[ 'user_pwd' ])
            {
                return json(array('code' => 1, 'msg' => "账号或密码错误!!!", 'data' => ''));
            }
            Session::set( 'user', $res );
            return json(array('code' => 0, 'msg' => "OK", 'data' => ''));

        
    }

    public function getList()
    {
        $page = request() ->get( 'page', 1);
        $limit = request() ->get( 'limit',10);
        $id = request()->get('id','');
        $status = request()->get('status','');


        $where = [];
        if( $id != '')
        {
            $where[ 'id' ] = $id;
        }

        if( $status != '')
        {
            $where[ 'status' ] = $status;
        }



        $res = model('D') ->getLists( $page, $limit, $where);
        if( !$res )
        {
            return json(array('code' => 1, 'msg' => "数据错误!!!", 'data' => ''));
        }
        
        
        return json(array('code' => 0, 'msg' => "OK", 'data' => $res ));
    }

    public function Dels()
    {
        $id = request()->delete('id','');

        if( $id == '' || $id == null )
        {
            return json(array('code' => 1, 'msg' => "数据错误!!!", 'data' => ''));  
        }

        $res = model('D') -> Del( $id );

        if( !$res )
        {
            return json(array('code' => 1, 'msg' => "数据错误!!!", 'data' => ''));
        }
        
        
        return json(array('code' => 0, 'msg' => "OK", 'data' => $res ));


    }
}